package org.karelun.gef.utils;

import java.util.List;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.karelun.gef.model.DiagramModel;
import org.karelun.gef.model.Node;

public class NodeUtils {
	
	public static boolean isOverlap(Node node,Point p,Dimension size){
		DiagramModel diagram = node.getDiagram();
		if(diagram==null)
			return false;
		List<Node> children = diagram.getNodes();
		if(children==null||children.isEmpty())
			return false;
		for(Node child:children){
			if(node.equals(child))
				continue;
			if(isOverlapIn(child,p,size))
				return true;
		}
		return false;
	}
	
	private static boolean isOverlapIn(Node child, Point p,Dimension size) {
		Point l = child.getLocation();
		Dimension s = child.getSize();
		int x1 = l.x;
		int x2=x1+s.width;
		int x3=p.x;
		int x4=p.x+size.width;
		
		int y1=l.y;
		int y2=y1+s.height;
		int y3=p.y;
		int y4=y3+size.height;
		
		return isOverlap(x1,x2,x3,x4)&&isOverlap(y1, y2, y3, y4);
	}
	
	private static boolean isOverlap(int a, int b, int c, int d) {
		return b>c&&a<d;
	}
}
